package geppetto.reordering;

import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;

public class ScoreWeightedMSDReorderingCalculator extends MSDReorderingCalculator{

	public ScoreWeightedMSDReorderingCalculator(REO_MODEL_TYPE modelType,AlignmentsSet set) {
		this(DEFAULTSMOOTHING,modelType, set);
	}

	public ScoreWeightedMSDReorderingCalculator(double smoothing, REO_MODEL_TYPE modelType,AlignmentsSet set) {
		super(smoothing, modelType, set);		
	}

	public ScoreWeightedMSDReorderingCalculator(AlignmentsSet set) {
		this(REO_MODEL_TYPE.REO_MSD, set);
	}

	@Override
	public ReorderingInfo getReorderingInfo(ExtractedPhrasePair epp){
		int sentenceNumber = epp.getSentenceNumber();
		int startS = epp.getSourceStartPos();
		int endS = epp.getSourceEndPos();
		int startT = epp.getForeignStartPos();
		int endT = epp.getForeignEndPos(); 

		Alignment alignment = set.get(sentenceNumber);
		boolean connectedLeftTopP = isAligned(alignment, startS-1, startT-1);
		boolean connectedRightTopP = isAligned(alignment,endS+1, startT-1 );
		boolean connectedLeftTopN = isAligned(alignment,endS+1, endT+1 );
		boolean connectedRightTopN = isAligned(alignment,startS-1, endT+1 ); 

		double probLeftP = connectedLeftTopP && !connectedRightTopP ? 1 : 0;
		double probRightP = connectedRightTopP && !connectedLeftTopP ? 1 : 0;
		double probUnkP = probLeftP == 0 && probRightP == 0 ? 1 : 0;
		double probLeftN = connectedLeftTopN && !connectedRightTopN ? 1 : 0;
		double probRightN = connectedRightTopN && !connectedLeftTopN ? 1 : 0;
		double probUnkN = probLeftN == 0 && probRightN == 0 ? 1 : 0; 

		ReorderingInfo info = new  ReorderingInfo();
		double prob = epp.getScore();
		info.addPrevWordOrientation(convertREO_POS(REO_POS.LEFT), probLeftP * prob);
		info.addPrevWordOrientation(convertREO_POS(REO_POS.RIGHT), probRightP * prob);
		info.addPrevWordOrientation(convertREO_POS(REO_POS.UNKNOWN), probUnkP * prob);
		info.addNextWordOrientation(convertREO_POS(REO_POS.LEFT), probLeftN * prob);
		info.addNextWordOrientation(convertREO_POS(REO_POS.RIGHT), probRightN * prob);
		info.addNextWordOrientation(convertREO_POS(REO_POS.UNKNOWN), probUnkN * prob);				
		return info;
	}
}
